<?php


namespace app\index\controller;

use app\index\model\Banner;
use app\index\model\Crane;
use app\index\model\Plan as PlanModel;
use app\index\model\News as NewsModel;
use app\index\model\GoodsCat;
use app\index\model\ProductionStage;
use app\index\model\SegmentType;
use think\admin\Controller;

class Plan extends Base
{
    /**
     * @name:今日计划
     * @author:Sam
     * @date:2024-09-02
     * @param:[string] param [desc]
    */
    public function plan_today()
    {

        $car_id =  request()->get('car_id/d',0);
        $where  = [];
        if($car_id > 0) $where[] = ['crane_id','=',$car_id];
        $this->carne_list       = Crane::select();
        $this->list = PlanModel::whereTime('start_time','<',time())->whereTime('end_time','>=',time())->with(['crane','segment','stage'])->where($where)->order('id asc')->append(['status_text','true_circle'])->select();
        $this->car_id = $car_id;
        return $this->fetch();
    }   

    //计划列表
    public function plan_list()
    {
        $car_id =  request()->get('car_id/d',0);
        $where  = [];
        if($car_id > 0) $where[] = ['crane_id','=',$car_id];
        $this->carne_list       = Crane::select();
        $this->list = PlanModel::where($where)->with(['crane','segment','stage'])->order('id desc')->append(['status_text'])->select();
        $this->car_id = $car_id;
        return $this->fetch();
    }   




    //新增计划
    public function plan_add(){
        if($this->app->request->isPost()){
            $post_data = $this->_vali([
                'crane_id.require'              => '吊车不能为空!',
                'segment_type_id.require'       => '分段类型不得为空!',
                'production_stage_id.require'   => '阶段不得为空!',
                'segment_type_no.require'       => '分段号不得为空!',
                'produce_no.require'            => '工程号不能为空!',
                'circle.require'                  => '标准周期不能为空!',
                'start_time.require'              => '计划开始时间不得为空!',
                'end_time.require'                => '计划结束时间不得为空!',
            ]);

            $post_data['remarks'] = input('remarks','');

            if(strtotime($post_data['end_time']) <= strtotime($post_data['start_time'])) return $this->error('结束时间不得小于开始时间');
            $post_data['start_time']  = date('Y-m-d H:i:s',strtotime($post_data['start_time']));
            $post_data['end_time']    = date('Y-m-d H:i:s',strtotime($post_data['end_time']));
            $post_data['create_time'] = date('Y-m-d H:i:s');
            $post_data['uid']         = $this->uid;
            if(PlanModel::create($post_data)) return $this->success('操作成功');
            return $this->error('操作失败');
        }else{
            $this->segment_type_list = SegmentType::select();
            $this->production_stage_list = ProductionStage::select();
            $this->carne_list       = Crane::select();
            return $this->fetch();
        }
        
    }


      //删除计划
      public function plan_delete(){
        if($this->app->request->isPost()){
            $post_data = $this->_vali([
                 'id.require'              => 'ID不能为空!'
            ]);
            if(!$detail = PlanModel::find($post_data['id'])) return $this->error('数据不存在');
            if($detail->delete()) return $this->success('操作成功');
            return $this->error('操作失败');
        }else{
            return $this->error('操作异常');
        }
        
    }

    //编辑计划
    public function plan_edit($id){
        if($this->app->request->isPost()){
            $post_data = $this->_vali([
                'crane_id.require'              => '吊车不能为空!',
                'segment_type_id.require'       => '分段类型不得为空!',
                'production_stage_id.require'   => '阶段不得为空!',
                'segment_type_no.require'       => '分段号不得为空!',
                'produce_no.require'            => '工程号不能为空!',
                'circle.require'                  => '标准周期不能为空!',
                'start_time.require'                    => '计划开始时间不得为空!',
                'end_time.require'                      => '计划结束时间不得为空!',
            ]);
      
            $post_data['remarks'] = input('remarks','');
            // $post_data['start_time']  = date('Y-m-d H:i:s',strtotime($post_data['start_time']));
            // $post_data['end_time']    = date('Y-m-d H:i:s',strtotime($post_data['end_time']));
        
            if(strtotime($post_data['end_time']) <= strtotime($post_data['start_time'])) return $this->error('结束时间不得小于开始时间');
            $post_data['start_time'] = date('Y-m-d H:i:s',strtotime($post_data['start_time']));
            $post_data['end_time'] = date('Y-m-d H:i:s',strtotime($post_data['end_time']));
            if(PlanModel::where('id',$id)->update($post_data)) return $this->success('操作成功');
            return $this->error('操作失败');
        }else{
            $this->detail = PlanModel::with(['crane','segment','stage'])->find($id);
            $this->segment_type_list = SegmentType::select();
            $this->production_stage_list = ProductionStage::select();
            $this->carne_list       = Crane::select();
            return $this->fetch();
        }
        
    }


    /**
     * @name:详情
     * @author:Sam
     * @date:2024-09-02
     * @param:[string] param [desc]
    */
    public function detail($id){
        $this->detail = NewsModel::where('status',1)->find($id);
        $this->next_id = NewsModel::where('status',1)->where('id','>',$id)->value('id');
        $this->pre_id = NewsModel::where('status',1)->where('id','<',$id)->value('id');
        return $this->fetch();
    }
}
